﻿<Window x:Class="ValidationRuleSetterWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:ValidationRuleExample"
    xmlns:v="http://wpfglue.wordpress.com/validation"
    Title="ValidationRuleSetterWindow" Height="400" Width="300">
    <Window.Resources>
        <v:TypeValidationRuleSetter x:Key="vrules" ReferenceType="{x:Type app:TestClass}">
            <v:PropertyValidationRuleSetter Path="StringProperty">
                <v:IsNotEmptyValidationRule Message="Must not be empty" ValidatesOnTargetUpdated="True"/>
            </v:PropertyValidationRuleSetter>
        </v:TypeValidationRuleSetter>
        <app:TestClass x:Key="dc"/>
    </Window.Resources>
    <StackPanel >
        <GroupBox x:Name="ValidationContainer"  Header="Validation by ValidationRuleSetter" DataContext="{Binding}">
            <StackPanel x:Name="validationHost" v:Validation.RuleSetter="{StaticResource vrules}">
                <StackPanel.BindingGroup>
                    <BindingGroup/>
                </StackPanel.BindingGroup>
                <TextBox Text="{Binding StringProperty, UpdateSourceTrigger=PropertyChanged}"
                 Validation.ErrorTemplate="{StaticResource ValidationTemplate}"/>
                <TextBox Text="{Binding IntegerProperty, UpdateSourceTrigger=PropertyChanged}"
                 Validation.ErrorTemplate="{StaticResource ValidationTemplate}"/>
                <TextBox Text="{Binding DoubleProperty, UpdateSourceTrigger=PropertyChanged}"
                 Validation.ErrorTemplate="{StaticResource ValidationTemplate}"/>
                <TextBlock TextWrapping="Wrap">Only valid values will be committed to the bound object.</TextBlock>
                <TextBlock>List of all error messages:</TextBlock>
                <ItemsControl ItemsSource="{Binding ElementName=validationHost, Path=(Validation.Errors)}" DisplayMemberPath="ErrorContent" Validation.ValidationAdornerSiteFor="{Binding ElementName=validationHost}"/>
            </StackPanel>
        </GroupBox>
        <TextBox Validation.ErrorTemplate="{StaticResource ValidationTemplate}">
            <TextBox.Text>
                <Binding Path="StringProperty" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <v:IsNotEmptyValidationRule Message="Must not be Empty" ValidatesOnTargetUpdated="True"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <TextBlock TextWrapping="Wrap">Validation through ValidationRule directly on the Binding.</TextBlock>
        <TextBox Text="{Binding Path=StringProperty, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock TextWrapping="Wrap">No validation. Use this to change the property back to invalid values.</TextBlock>
        <TextBox Text="{Binding Path=IntegerProperty, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource ValidationTemplate}"/>
        <TextBlock TextWrapping="Wrap">Textbox bound to an integer property with default validation.</TextBlock>
        <TextBox Text="{Binding Path=IntegerProperty, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource ValidationTemplate}"/>
        <TextBlock TextWrapping="Wrap">Textbox bound to an integer property with validation through exceptions.</TextBlock>
    </StackPanel>
</Window>
